package cn.kgc.web.servlet;

import cn.kgc.web.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/10/8
 * @description /student
 */
public class StudentServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 创建一个学生集合   添加3个学生信息  将学生信息以表格的形式展示在页面中
        // Student student = new Student();
        // student.setId(1);
        // student.setScore(90);
        // student.setName("lisi");
        // student.setAge(20);

        Student student = new Student()
                .setScore(90)
                .setId(1)
                .setAge(20)
                .setName("lisi");


        List<Student> students = Arrays.asList(student, student, student);



        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();

        writer.write(" <table border=\"1\" cellspacing=\"0\">");
        writer.write("<tr>");
        writer.write("<th>ID</th>");
        writer.write("<th>姓名</th>");
        writer.write("<th>分数</th>");
        writer.write("<th>年龄</th>");
        writer.write("</tr>");

        students.forEach(stu->{
            writer.write("<tr>");
            writer.write("<td>"+stu.getId()+"</td>");
            writer.write("<td>"+stu.getName()+"</td>");
            writer.write("<td>"+stu.getScore()+"</td>");
            writer.write("<td>"+stu.getAge()+"</td>");
            writer.write("</tr>");
        });

        writer.write("</table>");
        writer.close();
    }
}
